package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    public static RadioGroup main_radiogroup;
    private MainF1 mainF1;
    private MainF2 mainF2;
    private MainF3 mainF3;
    private MainF4 mainF4;


    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_radiogroup = (RadioGroup) findViewById(R.id.main_radiogroup);
        fragmentManager = getSupportFragmentManager();
        main_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fragmentTransaction = fragmentManager.beginTransaction();
                if (mainF1!=null){fragmentTransaction.hide(mainF1);}
                if (mainF2!=null){fragmentTransaction.hide(mainF2);}
                if (mainF3!=null){fragmentTransaction.hide(mainF3);}
                if (mainF4!=null){fragmentTransaction.hide(mainF4);}
                switch (checkedId){
                    case R.id.main_item1:
                        if (mainF1==null){
                            mainF1 = new MainF1();
                            fragmentTransaction.add(R.id.main_farmelayout,mainF1);
                        }
                        fragmentTransaction.show(mainF1);
                        break;
                    case R.id.main_item2:
                        if (mainF2==null){
                            mainF2 = new MainF2();
                            fragmentTransaction.add(R.id.main_farmelayout,mainF2);
                        }
                        fragmentTransaction.show(mainF2);
                        break;
                    case R.id.main_item3:
                        if (mainF3==null){
                            mainF3 = new MainF3();
                            fragmentTransaction.add(R.id.main_farmelayout,mainF3);
                        }
                        fragmentTransaction.show(mainF3);
                        break;
                    case R.id.main_item4:
                        if (mainF4==null){
                            mainF4 = new MainF4();
                            fragmentTransaction.add(R.id.main_farmelayout,mainF4);
                        }
                        fragmentTransaction.show(mainF4);
                        break;
                }
                fragmentTransaction.commit();
            }
        });
        main_radiogroup.check(R.id.main_item1);
    }

}